﻿
Ext.namespace('EHSubmit.Work.MarkingList');
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//作业信息列表

//列模式
EHSubmit.Work.MarkingList.ColumnModel = new Ext.grid.ColumnModel([
    { header: 'ID', hidden: true, hideable: false, dataIndex: 'ID' },
    { header: '作业ID', hidden: true, hideable: false, dataIndex: 'GoalID' },
    { header: '作业名', width: 200, sortable: true, dataIndex: 'GoalName' },
    { header: '学生ID', width: 150, hidden: false, hideable: false, dataIndex: 'StudentID' },
    { header: '提交时间', width: 120, sortable: true, renderer: Ext.util.Format.dateRenderer('Y年m月d日'), dataIndex: 'SubmitTime' },
    { header: '最近更新', width: 120, sortable: true, renderer: Ext.util.Format.dateRenderer('Y年m月d日'), dataIndex: 'UpdateTime' },
    { header: '附件', width: 40, sortable: true, renderer: EHSubmit.Format.homeworkAttachRenderer, dataIndex: 'AttachPath' },
    { header: '说明', width:100, sortable: false, dataIndex: 'Description' },
    { header: '得分', width:40, sortable: true, renderer:EHSubmit.Format.scoreRenderer, dataIndex: 'Scores' },
    { header: '评论', sortable: false, dataIndex: 'Comment' }
]);

//数据源
EHSubmit.Work.MarkingList.Store = new Ext.data.Store({
    baseParams: { 
        Op: 'list_teacher',
        goalID:'',
        type: 0
    },      // 需要传递操作参数
    proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'HomeworkHandler.ashx' }),  //后台具体处理

    reader: new Ext.data.JsonReader({
            totalProperty: 'count',    //总记录个数
            root: 'data',
            id: 'ID'
        },
        [   // 数据模型
            { name: 'ID' },
            { name: 'GoalID' },
            { name: 'GoalName' },
            { name: 'StudentID'},
            { name: 'SubmitTime' },
            { name: 'UpdateTime' },
            { name: 'AttachPath'},
            { name: 'Description'},
            { name: 'Scores' },
            { name: 'Comment' }
    ]),
    listeners: {
        //-----根据combo指定goalId时，翻页或刷新时刷新交的作业情况-------
        load: function(thisStore, records, opt) {
            Ext.Ajax.request({
                url: EHSubmit.ajaxRoot + 'GoalHandler.ashx',
                params: {
                    Op: 'CountStu',
                    courseID:EHSubmit.Work.MarkingList.courseID,
                    goalID: EHSubmit.Work.goalID
                },
                success: function(response, opts) {

                    //获取服务器端的数据(解析后返回的是对象)
                    var statistics = Ext.decode(response.responseText).statistics;
                    var difference = statistics.total - statistics.submited;
                    var tipsLabel = Ext.getCmp('markingHworksToGoal');
                    var tips = '作业需交人数为' + statistics.total + '人,已交人数为' + statistics.submited + '人,未交人数为' + difference + '人';
                    tipsLabel.setText(tips);
                }
                //      failure: function(response, opts){
                //          var result = Ext.decode(response.responseText).msg;
                //          Ext.MessageBox.alert('错误',result);
                //      },
            });
        },
        /// 加载异常处理
        exception: function(proxy, type, action, options, response){
            Ext.MessageBox.alert('数据加载错误!','请与管理员联系');
            
        }
    }
});

// 数据列表
EHSubmit.Work.MarkingList.Grid = {
    xtype: 'grid',
    id: 'ListMarkingHomeworkGrid',
    layout: 'fit',
    //autoHeight:true,
    height: 440,
    border: true,
    frame: true,
    loadMask: true,          // 加载数据时遮住
    autoExpandColumn: 8,     // 将最后一列宽度自动扩展
    cm: EHSubmit.Work.MarkingList.ColumnModel,
    ds: EHSubmit.Work.MarkingList.Store,
    //    sm: new Ext.grid.RowSelectionModel({
    //        singleSelect: true,              // 一次只选一个
    //        listeners: {
    //            rowselect: function(sm, rowIndex, rec) {

    //            }
    //        }
    //    }),

    // 工具条
    tbar: [
        '所教课程:',
        {
            id: 'markingCoursesCombo',
            xtype: 'combo',
            width: 200,
            editable: false,
            //value: 'UName',   //默认值
            emptyText: '请选择...',
            mode: 'remote',
            triggerAction: 'all',
            selectOnFocus: true,
            valueField: 'courseID',
            displayField: 'courseName',

            /*  
            * 关键点(网上资料)
            * 1. x-menu使combo在选择的时候不会隐藏掉menu,看Ext.menu.MenuMgr.onMouseDown源码即知  
            * 2. x-menu里面的"z-index: 15000;",因为menu里面的layer的z-index是15000,则这里大于15000(需要限制覆盖原来的文档元素element.style)即可..   
            * 如果实际用x-menu，其他有应用x-menu的会受到干扰，设置还需考虑适当的值
            */
            listClass: 'x-menu',

            store: new Ext.data.JsonStore({
                storeId: 'courseStore',
                //autoLoad: true,
                baseParams: { Op: 'List_Course' },     //根据教师id获取其教的课程
                proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'CourseHandler.ashx' }),  //后台具体处理

                root: 'courses',
                fields: [
                    { name: 'courseID', mapping: 'Id' },
                    { name: 'courseName', mapping: 'Name' }
                ]
            }),

            listeners: {
                select: function(combo, record, index) {
                    //根据班级课程id和教师id获取发布的作业通知
                    EHSubmit.Work.MarkingList.courseID = record.data.courseID;
                    Ext.getCmp('markingGoalsCombo').clearValue();
                    Ext.getCmp('markingGoalsCombo').enable();
                }
            }
        }, '-',
       '作业名',
        {
            id: 'markingGoalsCombo',
            xtype: 'combo',
            width: 230,
            disabled: true,     //初始化时不可用，选择课程后可用
            editable: false,
            //value: 'UName',   //默认值
            emptyText: '请选择...',
            mode: 'local',
            triggerAction: 'all',
            selectOnFocus: true,
            //pageSize:5,

            valueField: 'goalID',
            displayField: 'goalName',

            /*  
            * 关键点(网上资料)
            * 1. x-menu使combo在选择的时候不会隐藏掉menu,看Ext.menu.MenuMgr.onMouseDown源码即知  
            * 2. x-menu里面的"z-index: 15000;",因为menu里面的layer的z-index是15000,则这里大于15000(需要限制覆盖原来的文档元素element.style)即可..   
            * 如果实际用x-menu，其他有应用x-menu的会受到干扰，设置还需考虑适当的值
            */
            listClass: 'x-menu',

            store: new Ext.data.JsonStore({
                storeId: 'goalsStore',
                autoLoad: false,
                baseParams: { Op: 'List_SubGoals', courseID: '' },
                proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'GoalHandler.ashx' }),  //后台具体处理

                root: 'goals',
                fields: [
                    { name: 'goalID', mapping: 'ID' },
                    { name: 'goalName', mapping: 'Title' }
                ],
                listeners: {
                    beforeload: function(thisStore) {
                        thisStore.removeAll();
                    }
                }
            }),

            listeners: {
                enable: function(combo) {
                    //将班级课程的id传给服务器端
                    var store = combo.getStore();
                    store.setBaseParam('courseID', EHSubmit.Work.MarkingList.courseID);
                    store.load();
                },

                //根据goalId获取已交的作业情况，更新grid和label(load中),并且恢复bbar
                select: function(combo, record, index) {

                    //更新grid
                    EHSubmit.Work.goalID = record.data.goalID;
                    var gridStore = Ext.getCmp('ListMarkingHomeworkGrid').getStore();
                    gridStore.setBaseParam('goalID', EHSubmit.Work.goalID);
                    gridStore.load({ params: { start: 0, limit: 15} });

                    //-----这里将原来隐藏的刷新按钮恢复，填写页数处恢复可用----------
                    var bbar = Ext.getCmp('ListMarkingHomeworkGrid').getBottomToolbar();
                    bbar.inputItem.enable();
                    bbar.refresh.show();
                }
            }
            //--居右‘->’
        }, '->', {
            //交的作业情况 
            id: 'markingHworksToGoal',
            xtype: 'label'
}],
    bbar: {
        xtype: 'paging',
        pageSize: 15,                  //只是起计算页数的作用，不影响表面,内部影响start 和 limit
        store: EHSubmit.Work.MarkingList.Store,
        displayInfo: true,
        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        emptyMsg: '没有记录'
    },
    listeners: {
        cellclick: function(thisGrid, rowIndex, columnIndex, e) {
            if (columnIndex != 6) {
                // 根据选定的hworkid弹出批改窗口,可查看提交者消息
                var store = thisGrid.getStore();
                var lastStart = store.lastOptions.params.start;
                //要批改的作业信息不应从grid获取，因为批改的也可能不是当前页的作业
                EHSubmit.Work.hwIndex = lastStart + rowIndex;
                EHSubmit.Work.isMarking = true;
                EHSubmit.showWin(EHSubmit.Work.Mark.WinConfig);
            }
        }
    }

};

////////////////////////////////////////////////////////////////
EHSubmit.Work.MarkingList.Tab = {
    id: 'markingHwList',
    title: '批改作业(主)',
    bodyStyle: 'padding:5px;',
    closable:true,
    items: [EHSubmit.Work.MarkingList.Grid],
    listeners:{
        //-------界面初始化，每当激活时上次的数据不可见-----------
        render: function(thisPanel) { 
            var listHwGrid = Ext.getCmp('ListMarkingHomeworkGrid');
            var listHwGridStore = listHwGrid.getStore();
            var bbar = listHwGrid.getBottomToolbar();
            if( listHwGridStore.getTotalCount() > 0 ){
                listHwGridStore.removeAll();
                //-----对于bbar其实数据还在，实际上并没有消除！！！！！！-----
                //----以下更新的只是表像，在填写页数上写下后几页并查找数据依然会发现原来的数据--------
                bbar.updateInfo();
                bbar.afterTextItem.setText(String.format(bbar.afterPageText, 1));
                bbar.inputItem.setValue('1');
                bbar.inputItem.disable();   //基于上述原因将其设置为不可用
                bbar.first.setDisabled(true);
                bbar.prev.setDisabled(true);
                bbar.next.setDisabled(true);
                bbar.last.setDisabled(true);
                bbar.refresh.hide();
            }
            else{
                bbar.inputItem.disable();   //基于上述原因将其设置为不可用
                bbar.refresh.hide();
            }
        }
    }
};

